package org.rbac.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
 * 
*  <pre>    
* 类名称：SpringUtil 
* 类描述：   spring 工具类
* 创建人：陈国祥   (kingschan)
* 创建时间：2015-7-6 上午9:31:07   
* 修改人：Administrator   
* 修改时间：2015-7-6 上午9:31:07   
* 修改备注：   
* @version V1.0
* </pre>
 */
public class SpringUtil {
	/**
	 * 显式得到spring bean
	 * @param beanName
	 * @return
	 */
	public static Object getBeanByName(String beanName,HttpServletRequest request){
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(
		   request.getServletContext()
		    );
		return ac.getBean(beanName);

	}
	/**
	 * 显式得到spring bean
	 * @param beanName
	 * @return
	 */
	public static Object getBeanByName(String beanName,ServletContext content){
		ApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(content);
		return ac.getBean(beanName);

	}
	/**
     * 显式得到spring bean
     * @param beanName
     * @return
     */
	public static Object getBeanByName(String beanName){
	    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        return wac.getBean(beanName);

    }
}
